package com.sp2.dev.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-1-23
 * Time: 上午9:29
 * To change this template use File | Settings | File Templates.
 */
public class CharactEncodingFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(CharactEncodingFilter.class);

    private FilterConfig filterConfig = null;

    private String encoding = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("-----------CharactEncodingFilter----------");
        this.filterConfig = filterConfig;
        this.encoding = "utf-8";
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (encoding == null) {
            servletRequest.setCharacterEncoding(encoding);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        filterConfig = null;
        encoding = null;
    }
}
